package MyList;

import java.util.Arrays;

public class myList implements IList{
    private static int LENTH = 10;
    int[] arr;//顺序表
    int Size;//有效元素个数

    public myList() {
        arr = new int[LENTH];
        Size = 0;
    }

    //在末尾添加元素
    @Override
    public void add(int data) throws FullError{
        try {
            if(isFull()){
                throw new FullError("顺序表已满");
            }
        }catch (FullError e){
            e.printStackTrace();
            this.arr = Arrays.copyOf(this.arr,this.Size*2);//扩容
            System.out.println("已扩容！");
        }finally {
            this.arr[this.Size++] = data;
        }
    }

    //在指定位置插入元素
    @Override
    public void add(int pos, int data) throws FullError,PosError{
        try {
            if(isFull()){
                throw new FullError("顺序表已满");
            }
            if(pos < 0 || pos > this.Size){
                throw new PosError("下标位置非法");
            }
        }catch (FullError e){
            e.printStackTrace();
            this.arr = Arrays.copyOf(this.arr,this.Size*2);//扩容
            System.out.println("已扩容");
        }catch (PosError e){
            e.printStackTrace();
            return;
        } finally{
            for (int i = this.Size - 1; i >= pos; i--) {
                this.arr[i + 1] = this.arr[i];
            }
            this.arr[pos] = data;
            this.Size++;
        }
    }

    //判断是否包含某个元素
    @Override
    public boolean contains(int toFind) {
        for (int i = 0; i < this.Size; i++) {
            if(this.arr[i] == toFind){
                return true;
            }
        }
        return false;
    }

    //查找某个元素的位置
    @Override
    public int indexOf(int toFind) {
        for (int i = 0; i < this.Size; i++) {
            if(this.arr[i] == toFind){
                return i;
            }
        }
        return -1;
    }

    //获取pos位置的元素
    @Override
    public int get(int pos) throws PosError,EmptyError{
        try {
            if(isPosError(pos)){
                throw new PosError("下标位置非法");
            }
            if(isEmpty()){
                throw new EmptyError("顺序表为空");
            }
        }catch (PosError e){
            e.printStackTrace();
            return -1;
        }catch (EmptyError e){
            e.printStackTrace();
            return -1;
        }

        return this.arr[pos];
    }

    //改变下标pos值为value
    @Override
    public void set(int pos, int value) throws PosError{
        try {
            if(isPosError(pos)){
                throw new PosError("下标位置非法");
            }
        }catch (PosError e){
            e.printStackTrace();
            return;
        }

        this.arr[pos] = value;
    }

    //删除第一次出现的元素tomove
    @Override
    public void remove(int toRemove) throws EmptyError{
        try {
            if(isEmpty()){
                throw new EmptyError("表为空");
            }
        }catch (EmptyError e){
            e.printStackTrace();
            return;
        }
        int i = 0;
        while (i < this.Size && this.arr[i] != toRemove){
            i++;
        }
        if(i == this.Size){
            System.out.println("未找到该值，无法删除");
        }else {
            for (int j = i; j < this.Size - 1; j++) {
                this.arr[j] = this.arr[j + 1];
            }
            this.Size--;
            System.out.println("删除成功");
        }
    }

    @Override
    public int size() {
        return this.Size;
    }

    //清空顺序表
    @Override
    public void clear() {
        int i = 0;
        while(i < this.Size){
            this.arr[i++] = 0;
        }
        this.Size = 0;
    }

    //打印顺序表
    @Override
    public void display() throws EmptyError{
        try {
            if(isEmpty()){
                throw new EmptyError("表为空");
            }
        }catch (EmptyError e){
            e.printStackTrace();
            return;
        }
        for (int i = 0; i < this.Size; i++) {
            System.out.print(this.arr[i] + " ");
        }
    }

    //判断空表
    private boolean isEmpty(){
        return this.Size == 0;
    }

    //判断下标合法性
    private boolean isPosError(int pos){
        return pos >= this.Size || pos < 0;
    }

    @Override
    public boolean isFull() {
        return Size == arr.length;
    }
}
